package converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.InitialContext;

import dao.RoleDAO;
import entity.Role;

@FacesConverter("stringToRoleConverter")
public class StringToRoleConverter implements Converter {
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		RoleDAO roleDAO = null;
		try {
			InitialContext ctx = new InitialContext();
			roleDAO = (RoleDAO) ctx.lookup("java:global/PaidClinic/JPARoleDAO!dao.RoleDAO");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roleDAO.selectRoleByName(value);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) { 		
		return ((Role) value).toString();
	}
	
}
